<template>
<view>
<userinfo id="user" :options="user_options" @getUserscope="getUserInfo" @toLogin="getUserInfo"></userinfo>

<view class="container">
    <block v-for="(item, index) in address_list" :key="index">
        <view :class="'address_item ' + (address_id != '-1' ? 'active' : '')" @tap="useAddress($event)" :data-is_deliver="item.is_deliver" :data-address_id="item.adress_id" :data-index="index">
            <view :class="'unradio ' + (item.adress_id == address_id ? 'radio' : '')"></view>
            <view>
                <lang class="title" keywords="收货人"></lang><text class="title">：</text>
                <text>{{item.name}}</text>
                <block v-if="item.defaults == 1"><lang class="fr" :style="'color:' + mobile_head_color" keywords="【默认】"></lang></block>
            </view>
            <view>
                <lang class="title" keywords="手  机"></lang><text class="title">：</text>
                <text>{{item.phone_show}}</text>
            </view>
            <view>
                <lang class="title" keywords="地 址"></lang><text class="title">：</text>
                <text>{{item.show_address}}</text>
            </view>
            <view class="cr" v-if="store_id != 0 && address_id != '-1' && !item.is_deliver" style="font-size:24rpx;"><lang keywords="此地址不在配送范围内（距离X1km）" :X1="item.distance"></lang></view>
        </view>
    </block>
    <view class="empty_box" v-if="address_list.length == 0">
      <view>
        <text class="iconfont icon-zanwu"></text>
        <view class="txt"><lang keywords="暂无地址信息"></lang></view>
      </view>
    </view>

    <view class="btn-box">
      <button class="block_btn mlr20" @tap="add_address" :style="'background:' + mobile_head_color"><lang keywords="添加"></lang></button>
    </view>

</view>
</view>
</template>

<script>
import common from '../../utils/common.js';
var app = getApp();
var _this;
export default {
  data() {
    return {
      address_id: -1,
      store_id: 0,
      address_list: [],
      address: "",
      adss: false,
      isFromOther: '0',
      firstShow: false,
	  user_options: {},
	  mobile_head_color: ''
    };
  },

  onShow: function () {
    common.post('My&a=adress', {
      store_id: this.store_id
    }, "setPageContent", this);
	
	// #ifdef H5
	common.hideWxShare()
	// #endif
  },
  onLoad: function (options) {
	_this = this; 
    var ticket = wx.getStorageSync('ticket');

    if (!ticket) {
      _this.user_options = {
		  toLogin: true,
		  showCancel: false,
		  refuseIsShow: true
		}
    }

    if (options.address_id) {
	  _this.address_id = options.address_id;
	  _this.store_id = options.store_id ? options.store_id : 0;
      console.log('当前地址ID：' + this.address_id);
    }

    if (options.isFromOther) {
      wx.setNavigationBarTitle({
        title: common.changeTxt('常用地址')
      });
      this.isFromOther = '1';
    } else {
      wx.setNavigationBarTitle({
        title: common.changeTxt('收货地址')
      });
    }

    common.setMainColor(_this, function(config){
		_this.mobile_head_color = config.config.mobile_head_color
	});
  },
  methods: {
    useAddress: function (e) {
      console.log(_this.address_id);

      if (_this.address_id != -1) {
        console.log(e.currentTarget.dataset);

        if (_this.store_id == 0 || e.currentTarget.dataset.is_deliver) {
          wx.setStorageSync('use_address_id', e.currentTarget.dataset.address_id);

          if (_this.store_id == 0) {
            wx.setStorageSync('use_address_arr', _this.address_list[e.currentTarget.dataset.index]);
          }

          _this.address_id = e.currentTarget.dataset.address_id;
          setTimeout(function () {
            wx.navigateBack();
          }, 50);
        }
      } else {
        wx.navigateTo({
          url: "address_add?address_id=" + e.currentTarget.dataset.address_id + (_this.isFromOther == '1' ? '&isFromOther=1' : '')
        });
      }
    },
    add_address: function () {
      wx.navigateTo({
        url: "address_add" + (_this.isFromOther == '1' ? '?isFromOther=1' : '')
      });
    },
    setPageContent: function (result) {
      console.log(result);
      this.address_list = result;
      wx.hideToast();
    },
  }
};
</script>
<style>
@import "./address.css"
</style>